package org.springmvc.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springmvc.entity.User;
import org.springmvc.entity.enumeration.Gender;
import org.springmvc.entity.enumeration.Marriage;
import org.springmvc.mybatis.pagination.Page;
import org.springmvc.mybatis.pagination.Pageable;
import org.springmvc.service.UserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

/**
 * @author tangjialin on 2016-05-31 0031.
 */
@Controller
public class IndexController {

    @Resource
    private UserService userService;

    @RequestMapping("/")
    public String index(HttpServletRequest request) {
        return "index";
    }

    @GetMapping("/users/{userId}")
    @ResponseBody
    public ResponseEntity<User> findUser(@PathVariable("userId") Long userId) {
        return ResponseEntity.ok(userService.findCache(userId));
    }

    @GetMapping("/users/search")
    @ResponseBody
    public ResponseEntity<Page> search(Pageable pageable, Gender gender) {
        List<User> userList = userService.listByGender(pageable, gender);
        return ResponseEntity.ok(new Page<>(pageable, userList));
    }

    @GetMapping("/users/pagination")
    @ResponseBody
    public ResponseEntity<Page> pagination(Pageable pageable) {
        List<User> userList = userService.list(pageable);
        return ResponseEntity.ok(new Page<>(pageable, userList));
    }

    @PostMapping("/users")
    @ResponseBody
    public ResponseEntity<Void> insert() {
        int nextInt = Math.abs(new Random().nextInt());
        Map<String, Object> properties = new HashMap<>(4);
        properties.put("城市", "中国-北京");
        properties.put("level", 1);
        User user = new User()
                .setGender(Gender.values()[nextInt % Gender.values().length])
                .setMarriage(Marriage.values()[nextInt % Gender.values().length])
                .setOpenId(UUID.randomUUID().toString())
                .setProperties(properties);
        userService.insertOrUpdate(user);
        return ResponseEntity.noContent().build();
    }

    @PutMapping("/users")
    @ResponseBody
    public ResponseEntity<Void> update(@RequestBody User user) {
        user.setOpenId(UUID.randomUUID().toString());
        User exitsUser = userService.find(user.getId());
        userService.update(user.setVersion(exitsUser.getVersion()));
        return ResponseEntity.noContent().build();
    }

    @PostMapping("/refresh")
    @ResponseBody
    public ResponseEntity<Void> refresh() {
        userService.refresh();
        return ResponseEntity.noContent().build();
    }

}
